package com.ftwj.demo.modular.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ftwj.demo.base.util.PageInfoBean;
import com.ftwj.demo.config.swagger.DataType;
import com.ftwj.demo.config.swagger.HttpMethod;
import com.ftwj.demo.config.swagger.ParamType;
import com.ftwj.demo.modular.entity.DictBean;
import com.ftwj.demo.modular.service.DictService;
import com.ftwj.demo.global.shiro.ShiroUtil;
import com.ftwj.demo.utils.result.MessageEnum;
import com.ftwj.demo.utils.result.ResultBean;
import com.ftwj.demo.utils.validate.ValidateGroupKey;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
* <p>Title: DictController</p>  
* <p>Description: 字典管理前端控制器</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author HeYang
* @date 2020年4月4日 下午5:51:22
* @version V1.0
*/
@RestController 
@RequestMapping("/dict")
@Api(tags="字典管理接口", value="字典角色管理")
@Validated
public class DictController {

	@Autowired
	private DictService service;
	
	/**
	 * <p>Title: tree</p>  
	 * <p>Description: 字典树数据</p>  
	 * @param pid
	 * @param size
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@GetMapping("/tree")
	@ApiOperation(value="字典树数据", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="0")
		,@ApiImplicitParam(name="size", value="查询深度", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
	})
	public ResultBean<?> tree(@RequestParam(required = true)@PositiveOrZero Integer pid, @RequestParam(required = true)@Positive Integer size) {
		List<Map<String, Object>> list = service.findlistMap(pid, size);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: page</p>  
	 * <p>Description: 字典分页查询</p>  
	 * @param pid
	 * @param current
	 * @param size
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("/page")
	@ApiOperation(value="字典分页查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="0")
		,@ApiImplicitParam(name="current", value="当前页数", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="1")
		,@ApiImplicitParam(name="size", value="查询深度", dataType=DataType.INTEGER, paramType=ParamType.QUERY, defaultValue="10")
	})
	public ResultBean<?> page(@RequestParam(defaultValue="0")@PositiveOrZero Integer pid,@RequestParam(defaultValue="1")@Positive Integer current,@RequestParam(defaultValue="10")@Positive Integer size) {
		PageInfoBean pageInfo = new PageInfoBean();
		pageInfo.setPerPage(size).setPerPage(size);
		pageInfo = service.page(pageInfo,pid);
		return ResultBean.success(pageInfo);
	}
	
	/**
	 * <p>Title: list</p>  
	 * <p>Description: 字典列表查询</p>  
	 * @param pid
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	@GetMapping("/list/{pid}")
	@ApiOperation(value="字典列表查询", notes="数据",code=0,httpMethod=HttpMethod.GET)
	@ApiImplicitParams({
		 @ApiImplicitParam(name="pid", value="父ID", dataType=DataType.INTEGER, paramType=ParamType.PATH, defaultValue="0")
	})
	public ResultBean<?> list(@PathVariable@Positive(message="ID必须是大于0的整数") Integer pid) {
		List<DictBean> list = service.list(pid);
		return ResultBean.success(list);
	}
	
	/**
	 * <p>Title: insert</p>  
	 * <p>Description: 字典新增</p>  
	 * @param dict
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PostMapping("/insert")
	@ApiOperation(value="字典新增", notes="数据",code=0,httpMethod=HttpMethod.POST)
	public ResultBean<?> insert(@RequestBody@Validated DictBean dict) {
		dict.setCreateUser(ShiroUtil.getUserId()).setCreateTime(new Date());
		if(service.add(dict)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}

	/**
	 * <p>Title: update</p>  
	 * <p>Description: 字典修改</p>  
	 * @param dict
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@PutMapping("/update")
	@ApiOperation(value="字典修改", notes="数据",code=0,httpMethod=HttpMethod.PUT)
	public ResultBean<?> update(@RequestBody@Validated(ValidateGroupKey.class) DictBean dict) {
		dict.setCreateUser(ShiroUtil.getUserId()).setCreateTime(new Date());
		if(service.save(dict)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
	
	/**
	 * <p>Title: delete</p>  
	 * <p>Description: 字典删除</p>  
	 * @param id
	 * @author HeYang
	 * @date 2020年4月10日  
	 * @version 1.0
	 */
	@DeleteMapping("/delete/{id}")
	@ApiOperation(value="字典删除", notes="数据",code=0,httpMethod=HttpMethod.DELETE)
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="字典编号", dataType=DataType.INTEGER, paramType=ParamType.PATH)
	})
	public ResultBean<?> delete(@Positive(message="ID必须是大于0的整数")@PathVariable Integer id) {
		if(service.remo(id)>0) {
			return ResultBean.success();
		}else {
			return ResultBean.error(MessageEnum.SERVER_FAILED);
		}
	}
}
